package com.mall.seckill.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.ErrorMsg;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbSeckill;
import com.mall.seckill.mapper.SeckillItemMapper;
import com.mall.seckill.mapper.SeckillMapper;
import com.mall.seckill.service.SeckillService;
import com.mall.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author White
 * @version 1.0
 * @email 616769721@qq.com
 * @date 2021/7/17 20:00
 */
@Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private SeckillItemMapper seckillItemMapper;

    /**
     * 添加秒杀配置信息
     * @param tbSeckill
     * @return Boolean
     */
    @Override
    public CommonResponse addSeckill(TbSeckill tbSeckill) {
        try{
            //设置秒杀配置id
            tbSeckill.setId(new IdWorker().nextId());
            //设置更新时间
            tbSeckill.setCreateTime(new Date());
            ///执行添加
            seckillMapper.addSeckill(tbSeckill);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }

    }


    /**
     * 编辑秒杀配置
     * @param tbSeckill
     * @return Boolean
     */
    @Override
    public CommonResponse updateSeckill(TbSeckill tbSeckill) {
        try{
            ///执行添加
            seckillMapper.updateSeckill(tbSeckill);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),ErrorMsg.ERROR_OPERATION_404);
        }

    }

    @Override
    public TbSeckill hui(Long id) {
        return seckillMapper.hui(id);
    }

    @Override
    public CommonResponse updateStatus(Long id, String status) {
        try {
            seckillMapper.updateStatus(id,status);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(CommonStatus.ERROR.getCode(),ErrorMsg.ERROR_OPERATION_404);
        }
    }

    /**
     * 删除秒杀配置
     * 删除配置中的秒杀商品
     * @param id
     * @return Boolean
     */
    @Override
    public CommonResponse deleteById(Long id) {
        try{
            seckillMapper.deleteById(id);
            seckillItemMapper.deleteBySeckillId(id);
            return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
        }catch (Exception e){
            return new CommonResponse(CommonStatus.ERROR.getCode(), ErrorMsg.ERROR_OPERATION_404);
        }
    }

    @Override
    public PageInfo<TbSeckill> findList(TbSeckill tbSeckill, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<TbSeckill> list = seckillMapper.findList(tbSeckill);
//        List<TbSeckill> collect = list.stream().map(item -> {
//            item.setTimeFrame(new SimpleDateFormat("HH:mm").format(item.getStartTime()) + "-" + new SimpleDateFormat("HH:mm").format(item.getEndTime()));
//            return item;
//        }).collect(Collectors.toList());
        PageInfo<TbSeckill> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
